package org.novelhibernate.test;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import org.novelhibernate.core.ParameterMapper;
import org.novelhibernate.core.sqlregistry.AbstractSQLRegistry;
//import org.novelhibernate.hibernate.annotation.ClassAliasMapping;
import org.novelhibernate.util.SQLParser;
import org.hibernate.Hibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.hibernate.connection.C3P0ConnectionProvider;
import com.mchange.v2.c3p0.ComboPooledDataSource;


import junit.framework.TestCase;

public class NovelhibenrateTester  {
	
	public static final int pop=200;
	public static long[] is = new long[pop];

	  public static ApplicationContext contex;

	 public static int index=0;
	 public static  List l = new ArrayList();;
	 public static String query = "select message1.*, message.*   ,  message2.*   "
		 +" from "
                                                          +" (select * "
                                                        		  +" from Message "
                                                          +" where receiver=? "
                                                        		  +" order by sendtime desc) as message "
                                                          +" left join "
                                                          +"(select id,name,pic "
                                                        		  +"from t_vlemember) as member "
                                                          +"on message.sender=member.id"; 
	public  static void main(String[] a){
	
		
		//AbstractSqlRegistry ar = AbstractSqlRegistry.getInstance();
		
		//ar.registerUpdateSql("              update enmaterial  as   en set title  =   ?,  description  =    ?  where en.id   =   ?  and categoryid  = ?  ", EnMaterial.class,
			//	null);
		/*
		contex=new ClassPathXmlApplicationContext("/configurations/spring/applicationContext*.xml"); 
	
		EnMaterialDAO e = (EnMaterialDAO)NovelhibenrateTester.contex.getBean("enMaterialDAO");
		
		EnCategory c = new EnCategory();
		c.setId("CBDC9E2861478846E7F7B4FC7DC14C2B");
		
		EnMaterial m = new EnMaterial();
	        m.setId("007886908277A257CBFFB4FE4FA2C757");
	        m.setContent("test");
	        m.setDescription("1.111111");
	        m.setTitle("test");
	        m.setType("test");
	        m.setNumber(1);
	        m.setEnCategory(c);
	       // m.setMember(me);
	       // m.setPostTime(r);
		
	        EnMaterial m1 = new EnMaterial();
	        m1.setId("00f5a2c8df2943c99b41876f19a7409a");
	        m1.setTitle("test");
	        m1.setDescription("2.222222");
	        m1.setEnCategory(c);
	        
		
		//SqlParser s = SqlParser.getInstance();
		
		//System.out.print("select   e from    enn    where    id = ? and name in (?,?,?) and nn in (select e from eee where xid = ?)".replaceAll("\\(\\s*\\?", ""));
		
		try {
			e.jdbcSave3(new EnMaterial[]{m,m1});
			//Object[] o= s.parseUpdateSQL("   update enmaterial as en set title =  ?,  description  = ?     where ff in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) or  en.r in (select  r from rr where   f=?   and en.f1=? or   en.r=9) or  en.id =? and    en.name.nee= ?");
		
		   // for (Object ob : o)
		    	//System.out.print(((ParameterMapper)ob).getIndex() + ":"  + ((ParameterMapper)ob).getParameter() + "\n");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/saveTest();
	}
	
	public static void saveTest(){
		
		contex=new ClassPathXmlApplicationContext("/configurations/spring/applicationContext*.xml");
		//MessageDAO e = (MessageDAO)NovelhibenrateTester.contex.getBean("messageDAO");
	       List l=null;
		try {
			//l = e.testFind();
		} catch (Throwable e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	       for (int i=0;i<l.size();i++){
	    	  
	    	  // for(int j=0;j<((List)l.get(i)).size();j++){
	    		 //  System.out.print(((List)l.get(i)).get(j)+"\n");
	    	  for(int j=0;j<((Object[])l.get(i)).length;j++){
	    		  System.out.print(((Object[])l.get(i)).length);
	      // System.out.print(((Message)((Object[])l.get(i))[j]).getMemberBySender().getName()+"\n");
	      // System.out.print(((Object[])l.get(i)).length+"\n");
	    	  }
	       }/*/
		
			SqlParser parser = SqlParser.getInstance(); 
			
			String partialAliases = parser.parseJoinQuery(query.toLowerCase());
			String[] classAndAliases = new String[]{"message","message2","message1"};
			String filter = "";
			String fullEntityAlias = "";
			for (String classAndAlias : classAndAliases ){
				
				fullEntityAlias = classAndAlias;
				
				// filter the full constructed entities
				    filter = partialAliases.replace(fullEntityAlias + ".*,", "");
				if (filter.length() == partialAliases.length())
					filter = partialAliases.replace("," + fullEntityAlias + ".*", "");
				if (filter.length() == partialAliases.length())
					filter = partialAliases.replace(fullEntityAlias + ".*", "");
				
				partialAliases = filter;
				
				//query.addEntity(fullEntityAlias, classAndAlias.clazz());
			}
			
			List l = new ArrayList();
			l.add(2);
			l.add(0,1);
			 System.out.print(l.size()+"\n");*/
		
	}
	
	

}
